package com.yunshu.yunshuapp.tongxunlu;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yunshu.yunshuapp.ImageActivity;
import com.yunshu.yunshuapp.OnTapListener;
import com.yunshu.yunshuapp.R;
import com.yunshu.yunshuapp.utils.YSUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhouwenjie on 2017/5/6.
 */

public class VCardsListAdaptor extends RecyclerView.Adapter implements OnTapListener {

    private final ImageLoader mImageLoader;
    private final DisplayImageOptions options;
    private ArrayList<String> models;
    public OnTapListener tapListener;
    public AppCompatActivity activity;

    public VCardsListAdaptor(Context context){
        // 使用ImageLoader之前初始化
        YSUtils.initImageLoader(context);

        models = new ArrayList<String>();
        mImageLoader = ImageLoader.getInstance();
        options = YSUtils.displayImageOptions();
        tapListener = this;

    }

    @Override
    public int getItemCount() {

        int count = models.size();
        return count ;
    }

    @Override
    public int getItemViewType(int position) {

        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_list_item,parent,false);

        VCardsListAdaptor.ListViewHolder holder = new VCardsListAdaptor.ListViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        String model = models.get(position);
        ((VCardsListAdaptor.ListViewHolder)holder).setPosition(position);

        VCardsListAdaptor.ListViewHolder listHolder = (VCardsListAdaptor.ListViewHolder) holder;
        listHolder.setImageURL(model);

    }

    @Override
    public void onSingleTap(View v, int position) {

        String model =  this.models.get(position);
        Intent intent = new Intent();
        intent.setClass(activity, ImageActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("imgUrl",model);
        intent.putExtras(bundle);
        activity.startActivityForResult(intent,0);
    }


    class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView imgView;
        int mPosition;

        public ListViewHolder(View itemView ) {
            super(itemView);
            itemView.setOnClickListener(this);
            imgView = (ImageView) itemView.findViewById(R.id.imgView);
            ViewGroup.LayoutParams params = imgView.getLayoutParams();
            params.height = (YSUtils.screenWidth(activity) - 20) * 4 /3;
            imgView.setLayoutParams(params);

        }

        public void setPosition(int position) {
            mPosition = position;
        }


        public void onClick(View v) {
            tapListener.onSingleTap(v,mPosition);
            //    Toast.makeText(MainActivity.this, "click "+mPosition, 0).show();

        }

        public void setImageURL(String imageURL) {
//            this.imageURL = imageURL;
            mImageLoader.displayImage(imageURL, imgView);

        }
    }

    public void setModels(String[] newModels){
        models.clear();
        for (String url : newModels)
            models.add(url);
    }


}
